package dictionary

import (
    "time"

    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    "iip/dictionary/model"
)

func (s *Dictionary) MaterialCreate(c *gin.Context) {
    var req model.DictMaterial
    if err := c.BindJSON(&req); err != nil {
        s.json_error(c, err.Error())
        return
    }

    req.ID = s.idgen.NextID()
    req.CreatedAt = time.Time{}
    req.UpdatedAt = time.Time{}
    req.DeletedAt = gorm.DeletedAt{}
    if err := s.gorm().Create(&req).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    s.logger.Info("create material done")
    s.json_data(c, req)
}

func (s *Dictionary) MaterialDelete(c *gin.Context) {
    req := &struct {
        ID int64 `uri:"id"`
    }{}
    if err := c.ShouldBindUri(req); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    if err := s.gorm().Debug().Delete(&model.DictMaterial{ID: req.ID}).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    s.logger.Info("delete material by id ", req.ID)
    s.json_ok(c)
}

func (s *Dictionary) MaterialUpdate(c *gin.Context) {

    req_id := &struct {
        ID int64 `uri:"id"`
    }{}
    if err := c.ShouldBindUri(req_id); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    loc, err := s.GetMaterialByID(req_id.ID)
    if err != nil {
        s.json_error(c, err.Error())
        return
    }


    var row model.DictMaterial
    if err := c.BindJSON(&row); err != nil {
        s.json_error(c, err.Error())
        return
    }
    row.ID = loc.ID
    row.CreatedAt = loc.CreatedAt
    row.UpdatedAt = loc.UpdatedAt
    row.DeletedAt = loc.DeletedAt

    if err := s.gorm().Save(&row).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    } else {
        s.logger.Info("update material id=", row.ID)
        s.json_data(c, row)
    }
}

func (s *Dictionary) MaterialPagedList(c *gin.Context) {

    query := &struct {
        Page     int    `form:"page" binding:"required,gte=1" json:"page"`
        PageSize int    `form:"pagesize" binding:"required,gte=1" json:"pagesize"`
        Order    string `form:"order" json:"order"`
    }{}
    if err := c.ShouldBindQuery(query); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    if query.Order != "asc" && query.Order != "desc" {
        query.Order = "asc"
    }


    var total int64
    if err := s.gorm().Model(&model.DictMaterial{}).Count(&total).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    var lst []*model.DictMaterial
    if err := s.gorm().Limit(query.PageSize).Offset(query.PageSize * (query.Page - 1)).Order("id " + query.Order).Find(&lst).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    } else {
        s.logger.Info("query material paged list")
        s.json_paged_data(c, total, query.Page, query.PageSize, lst)
    }
}

func (s *Dictionary) MaterialByID(c *gin.Context) {
    req := &struct {
        ID int64 `uri:"id"`
    }{}
    if err := c.ShouldBindUri(req); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    row := model.DictMaterial{}
    if err := s.gorm().Debug().Where(&model.DictMaterial{ID: req.ID}).First(&row).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    } else {
        s.logger.Info("query material by id ", req.ID)
        s.json_data(c, row)
        return
    }
}
